package com.swh.dao;

import com.swh.entity.Meeting;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/11/25 16:52
 * @Version: 1.0
 */
public class MeetingDao extends BaseDao {

    // 总记录数
    public Integer countByTitleAndStatus(String title, Integer status) {
        String str = "";
        Object[] arge = null;
        if (status == -1) {
            str = "select count(*) from meeting where title like ?";
            arge = new Object[]{"%" + title + "%"};
        } else {
            str = "select count(*) from meeting where title like ? and status=?";
            arge = new Object[]{"%" + title + "%", status};
        }
        return jdbcTemplate.queryForObject(str, Integer.class, arge);
    }

    // 当前页的数据
    public List<Meeting> findPage(String title, Integer status, Integer startIndex, Integer pageSize) {

        String str = "";
        Object[] args = null;
        if (status == -1) {
            str = "select * from meeting where title like ? order by publish_date desc limit ?,?";
            args = new Object[]{"%" + title + "%", startIndex, pageSize};
        } else {
            str = "select * from meeting where title like ? and status = ? order by publish_date DESC  limit ?,?";

            args = new Object[]{"%" + title + "%", status, startIndex, pageSize};
        }
        return jdbcTemplate.query(str, new BeanPropertyRowMapper<>(Meeting.class), args);
    }

    //添加会议
    public void save(Meeting meeting) {
        jdbcTemplate.update("insert into meeting values(null,?,?,?,?,?,?,?,?,?)",
                meeting.getDeptName(),
                meeting.getDeptId(),
                meeting.getTitle(),
                meeting.getContent(),
                meeting.getPublishDate(),
                meeting.getStartTime(),
                meeting.getEndTime(),
                meeting.getStatus(),
                meeting.getMakeUser()
        );
    }


    // 查询会议详情
    public Meeting findById(long id) {
        try {
            return jdbcTemplate.queryForObject(
                    "select * from meeting where id=?",
                    new BeanPropertyRowMapper<>(Meeting.class), id);
        } catch (EmptyResultDataAccessException exception) {
            return null;
        }
    }

    // 实到人数
    public Integer countJoinMeetingByMeetingId(long id) {
        try {
            return jdbcTemplate.queryForObject(
                    "select count(1) from meeting_join where c_id=?",
                    Integer.class, id);
        } catch (EmptyResultDataAccessException exception) {
            return null;
        }
    }

    // 查询原来是否有参加过这个会议
    public Integer countByUserIdAndMeetingId(Long userId, long meetingId) {
        try {
            return jdbcTemplate.queryForObject(
                    "select count(1) from meeting_join where u_id=? and c_id=?",
                    Integer.class, userId, meetingId);
        } catch (EmptyResultDataAccessException exception) {
            // 如果是没有查询到则返回null
            return null;
        }
    }

    //参加会议
    public void joinMeeting(Long userId, Long meetingId) {
        jdbcTemplate.update("insert into meeting_join values(?,?)", userId, meetingId);
    }

    //取消会议
    public void unJoinMeeting(Long userId, long meetingId) {
        jdbcTemplate.update("delete from meeting_join where u_id=? and c_id=? ", userId, meetingId);
    }
}
